<!--模块：政策库 - 政策发布 - 发布-->
<template>
  <div class="policy-publish" style="overflow:scroll;height:100%">
    <meta name="referrer" content="no-referrer" />
    <AppMainContent style="width:100%;margin-top:0px;margin-left:0px;">
      <el-row style="background-color:#fff">
        <el-col :span="2" style="float:right;margin-bottom:8px;margin-top:8px">
          <el-button
            type="primary"
            @click="save('ruleForm')"
            style="width:100px"
          >
            保存
          </el-button>
        </el-col>
        <el-col :span="2" style="float:right;margin-bottom:10px;margin-top:8px">
          <el-button
            type="primary"
            @click="issue('ruleForm')"
            style="width:100px"
          >
            发布
          </el-button>
        </el-col>
      </el-row>
    </AppMainContent>

    <AppMainContent>
      <!-- 卡片头部 -->
      <el-card style="height:660px">
        <CardFormTitle title="政策信息" />
        <!-- 表单内容 -->

        <CardFormContent>
          <el-form
            :model="formInfo"
            :rules="rules"
            ref="ruleForm"
            style="width:1300px"
            class="demo-ruleForm"
          >
            <div class="formdata">
              <el-form-item label="政策标题" prop="title">
                <el-input
                  v-model="formInfo.title"
                  style="width:500px"
                ></el-input>
              </el-form-item>
            </div>
            <div class="formdata">
              <el-form-item label="政策等级" prop="level">
                <el-select v-model="formInfo.level">
                  <el-option
                    v-for="grade in dataOptions"
                    :key="grade.value"
                    :label="grade.desc"
                    :value="grade.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </div>
            <div class="formdata">
              <el-form-item label="政策类型" prop="type">
                <el-select v-model="formInfo.type">
                  <el-option
                    v-for="type in dataTypeOptions"
                    :key="type.value"
                    :label="type.desc"
                    :value="type.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </div>
            <div class="formdata">
              <el-form-item label="政策来源" prop="source">
                <el-input
                  style="width:250px"
                  v-model="formInfo.source"
                ></el-input>
              </el-form-item>
            </div>
            <div class="formdata">
              <el-form-item label="政策发布时间" prop="awardTime">
                <el-date-picker
                  v-model="formInfo.awardTime"
                  type="datetime"
                  placeholder="选择日期时间"
                  value-format="yyyy-MM-dd HH:mm:ss"
                ></el-date-picker>
              </el-form-item>
            </div>
            <div class="vueEditor">
              <div
                style="font-size:16px;font-weight:bold;margin-bottom:13px;display:none"
              >
                富文本编辑器内容：{{ formInfo.info }}
              </div>
              <VueQuillEditor
                :content.sync="formInfo.info"
                :business-type="1"
              />
            </div>
          </el-form>
        </CardFormContent>
      </el-card>
    </AppMainContent>
  </div>
</template>

<script>
//引入下拉框信息
import { policyLevelInfo, addSave, updatePolicy } from '@/api/policy/index'

export default {
  name: 'PolicyPublishAdd',
  data() {
    return {
      formInfo: {
        title: '',
        level: '',
        type: '',
        source: '',
        awardTime: '',
        info: '',
        // 0 正常  1 已经删除
        delFlag: 0,
        //创建时间
        createTime: '',
        createUserId: '',
        createUserName: '',
        //提交人ID 提交人姓名，提交时间
        commitUserId: '',
        commitUserName: '',
        commitTime: '',
        //发布时间
        publishTime: '',
      },
      //*等级下拉选项数据
      dataOptions: [],
      dataTypeOptions: [],
      rules: {
        title: [
          { required: true, message: '请输入政策标题', trigger: 'blur' },
          {
            min: 2,
            max: 100,
            message: '长度在 2 到 100 个字符',
            trigger: 'blur',
          },
        ],
        level: [{ required: true, message: '请选政策等级', trigger: 'change' }],
        type: [
          { required: true, message: '请选择政策类型', trigger: 'change' },
        ],
        source: [
          { required: true, message: '请输入政策来源', trigger: 'blur' },
        ],
        awardTime: [
          {
            type: 'string',
            required: true,
            message: '请选择时间',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    //*保存数据
    save(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let that = this
          that.formInfo.createUserId = this.$store.state.user.userInfo.id
          that.formInfo.createUserName = this.$store.state.user.userInfo.userName
          that.formInfo.commitUserName = this.$store.state.user.userInfo.userName
          that.formInfo.commitUserId = this.$store.state.user.userInfo.id
          let data = that.formInfo
          addSave(data).then((response) => {
            if (response.code === 200 && response.msg == '新增成功') {
              that.$alert(that.formInfo.title + '新增成功', '消息', {
                confirmButtonText: '确定',
                callback: (action) => {
                  window.location.href = '/policy/publish'
                },
              })
            }
          })
        } else {
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    //*待审核状态 发布政策
    issue(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let that = this
          that.formInfo.createUserId = this.$store.state.user.userInfo.id
          that.formInfo.createUserName = this.$store.state.user.userInfo.userName
          that.formInfo.commitUserName = this.$store.state.user.userInfo.userName
          that.formInfo.commitUserId = this.$store.state.user.userInfo.id
          let data = that.formInfo
          updatePolicy(data).then((response) => {
            if (response.code === 200 && response.msg == '发布成功') {
              that.$alert(that.formInfo.title + '发布成功', '消息', {
                confirmButtonText: '确定',
                callback: (action) => {
                  window.location.href = '/policy/publish'
                },
              })
            }
          })
        } else {
          return false
        }
      })
    },

    //* 获取级别下拉框信息
    getLevelOptions() {
      policyLevelInfo()
        .then((response) => {
          if (response.code === 200) {
            this.dataOptions = response.data[0]
            this.dataTypeOptions = response.data[1]
          }
        })
        .catch(function(error) {
          this.$message.error('系统错误')
        })
    },
    //* 处理当前时间
    addDate() {
      const nowDate = new Date()
      const date = {
        year: nowDate.getFullYear(),
        month: nowDate.getMonth() + 1,
        date: nowDate.getDate(),
        hour: nowDate.getHours(),
        mf:
          nowDate.getMinutes() < 10
            ? '0' + nowDate.getMinutes()
            : nowDate.getMinutes(),
        ss: nowDate.getSeconds(),
      }
      const newmonth = date.month > 10 ? date.month : '0' + date.month
      const day = date.date > 10 ? date.date : '0' + date.date
      this.formInfo.publishTime =
        date.year +
        '-' +
        newmonth +
        '-' +
        day +
        ' ' +
        date.hour +
        ':' +
        date.mf +
        ':' +
        date.ss
      this.formInfo.createTime = date.year + '-' + newmonth + '-' + day
      this.formInfo.commitTime =
        date.year + '-' + newmonth + '-' + day + ' ' + date.hour + ':' + date.mf
    },
  },
  created() {
    this.getLevelOptions()
  },

  mounted() {
    this.addDate()
  },
}
</script>

<style lang="scss" scoped>
.formdata {
  float: left;
  margin-left: 30px;
  margin-top: 15px;
}

.vueEditor {
  float: left;
  margin-top: 30px;
  margin-left: 30px;
}
</style>
